package com.yy.micro.magic.data.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yy.micro.magic.core.model.PlanSchedule;
import com.yy.micro.magic.data.entity.MagicRuleEntity;
import com.yy.micro.magic.data.entity.MagicPlanScheduleEntity;
import com.yy.micro.magic.data.model.vo.JpaObjectWithDynamicFieldsVo;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class MagicPlanScheduleVo extends JpaObjectWithDynamicFieldsVo {

    private Long planId;
    private String planCode;
    private String category;
    private String description;
    private String version;
    private boolean disable;
    private Set<Long> ruleList = new LinkedHashSet<>();

    private List<MagicRuleVo> rules;

    public Long getPlanId() {
        return planId;
    }

    public void setPlanId(final Long planId) {
        this.planId = planId;
    }

    public String getPlanCode() {
        return planCode;
    }

    public void setPlanCode(final String planCode) {
        this.planCode = planCode;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }


    public boolean isDisable() {
        return disable;
    }

    public void setDisable(final boolean disable) {
        this.disable = disable;
    }

    public Set<Long> getRuleList() {
        return ruleList;
    }

    public void setRuleList(Set<Long> ruleList) {
        if (!CollectionUtils.isEmpty(ruleList)){
            if (CollectionUtils.isEmpty(this.ruleList)){
                this.ruleList = ruleList;
            }else{
                this.ruleList.addAll(ruleList);
            }
        }
    }

    public MagicPlanScheduleVo addRule(Long ruleId){
        if (null != ruleId){
            if (CollectionUtils.isEmpty(ruleList)){
                ruleList = new HashSet<>();
            }
            ruleList.add(ruleId);
        }
        return this;
    }

    public List<MagicRuleVo> getRules() {
        return rules;
    }

    @JsonIgnore
    public List<MagicRuleEntity> getRuleVos() {
        if (null != this.rules){
            List<MagicRuleEntity> rs = new ArrayList<>();
            this.rules.forEach(r->{
                rs.add(r.to());
            });
            return rs;
        }
        return null;
    }



    public void setRules(List<MagicRuleVo> rules) {
        this.rules = rules;
    }
    @JsonIgnore
    public void setRuleVos(List<MagicRuleEntity> rules) {
        if (null != rules){
            this.rules = new ArrayList<>();
            rules.forEach(r->{
                this.rules.add(MagicRuleVo.create().from(r));
            });
        }
    }

    @JsonIgnore
    public static MagicPlanScheduleVo create(){
        return new MagicPlanScheduleVo();
    }

    @JsonIgnore
    public MagicPlanScheduleVo from(MagicPlanScheduleEntity from){
        super.from(from);
        setCategory(from.getCategory());
        setDescription(from.getDescription());
        setDisable(from.isDisable());
        setPlanId(from.getPlanId());
        setPlanCode(from.getPlanCode());
        setRuleList(from.getRuleList());
        setRuleVos(from.getRules());
        setVersion(from.getVersion());
        return this;
    }

    @JsonIgnore
    public MagicPlanScheduleEntity to(){
        MagicPlanScheduleEntity to = new MagicPlanScheduleEntity();
        super.to(to);
        to.setCategory(getCategory());
        to.setRules(getRuleVos());
        to.setRuleList(getRuleList());
        to.setDisable(isDisable());
        to.setPlanId(getPlanId());
        to.setPlanCode(getPlanCode());
        to.setDescription(getDescription());
        to.setVersion(getVersion());
        return to;
    }

    @JsonIgnore
    public PlanSchedule toDomain(){
        PlanSchedule schedule = new PlanSchedule();
        schedule.setDescription(getDescription());
        schedule.setPlanCode(getPlanCode());
        if (!StringUtils.isEmpty(version)){
            schedule.setVersion(Double.parseDouble(version));
        }
        if (!CollectionUtils.isEmpty(rules)){
            rules.forEach(r->{
                schedule.addRule(r.toDomain());
            });
        }
        return schedule;
    }
}
